package com.ruoyi.mateProcurement.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.company.domain.Company;
import com.ruoyi.company.mapper.CompanyMapper;
import com.ruoyi.mateProcurement.domain.MateOutbound;
import com.ruoyi.mateProcurement.domain.MateOutboundModel;
import com.ruoyi.mateProcurement.domain.MateProcurement;
import com.ruoyi.mateProcurement.domain.MateProcurementModel;
import com.ruoyi.mateProcurement.service.MateOutboundService;
import com.ruoyi.mateProcurement.service.MateProcurementService;
import com.ruoyi.mateWarehousing.domain.MateWarehousing;
import com.ruoyi.material.domain.MateProductModel;
import com.ruoyi.material.domain.MaterialManufactuter;
import com.ruoyi.material.domain.MaterialProduct;
import com.ruoyi.material.service.MateManufactuterService;
import com.ruoyi.material.service.MateProductService;
import com.ruoyi.sale.domain.SaleorderModel;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.warehousing.domain.Warehousing;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author
 * @Version 1.0
 */
@RestController
@RequestMapping("/mateOutbound")
public class MateOutboundController extends BaseController {

    @Autowired
    MateProductService mateProductService;
    @Autowired
    MateProcurementService mateProcurementService;

    @Autowired
    CompanyMapper companyMapper;

    @Autowired
    MateManufactuterService mateManufactuterService;

    @Autowired
    SysUserMapper sysUserMapper;

    @Autowired
    MateOutboundService mateOutboundService;

    /**
     * 新增保存出库信息
     */
    @RequiresPermissions("business:procurement:add")
    @PostMapping("/add")
    public AjaxResult addSave(@RequestBody MateOutbound mateOutbound) {
        if (mateOutbound.getMateOutboundModels() == null) {
            return AjaxResult.error("请选择需要出库的原材料");
        }
        String mateGoodsNum = mateOutbound.getMateGoodsNum();
        Long companyId = SecurityUtils.getLoginUser().getCompanyId();
        Map<String, Object> resMap = new HashMap<>();
        resMap.put("mateGoodsNum", mateGoodsNum);
        resMap.put("deptId", companyId);
        MateOutbound mateOutbound1 = mateOutboundService.selectMateOutbound(resMap);
        if (mateOutbound1 != null) {
            //使用SimpleDateFormat的parse()方法生成Date
            SimpleDateFormat a = new SimpleDateFormat("yyyy-MM-dd");
            Date dates = null;
            try {
                dates = a.parse(a.format(mateOutbound1.getMateGoodsDate()));
            } catch (ParseException e) {
                e.printStackTrace();
            }
            String s = mateProcurementService.orderNum(dates, 1);
            mateOutbound.setMateGoodsNum(s);
        }
        mateOutboundService.insertMateOutbound(mateOutbound);
        return AjaxResult.success("添加成功");
    }

    @GetMapping("/remove/{mateGoodsId}")
    public AjaxResult removeProcurement(@PathVariable("mateGoodsId") Long mateGoodsId) {
        mateOutboundService.deleteMateOutbound(mateGoodsId);
        return AjaxResult.success("删除成功");
    }

    @PostMapping("/edit")
    public AjaxResult updateProcurement(@RequestBody MateOutbound mateOutbound) {
        mateOutboundService.updateMateOutbound(mateOutbound);
        return AjaxResult.success("修改成功");
    }

    /**
     * 查询订单入库信息列表
     */
    @RequiresPermissions("business:procurement:list")
    @GetMapping("/list")
    @ResponseBody
    public TableDataInfo list(@RequestParam(value = "beginTime", required = false) String beginTime,
                              @RequestParam(value = "endTime", required = false) String endTime) {
        MateOutbound mateOutbound = new MateOutbound();
        mateOutbound.setBeginTime(beginTime);
        mateOutbound.setEndTime(endTime);
        startPage();
        mateOutbound.setDeptId(SecurityUtils.getLoginUser().getUser().getCompanyId());
        List<MateOutbound> list = mateOutboundService.selectMateOutboundList(mateOutbound);
        return getDataTable(list);
    }

    @GetMapping("/selectEdit")
    public Map<String, Object> selectEdit(@RequestParam(value = "mateGoodsId", required = false) Long mateGoodsId,
                                          @RequestParam(value = "materialId", required = false) Long materialId) {
        Map<String, Object> resMap = new HashMap<>();
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        Company company = companyMapper.selectByCompanyId(sysUser.getCompanyId());
        resMap.put("taxRate", company.getTaxRate());
        MateOutbound mateOutbound = new MateOutbound();
        mateOutbound.setMateGoodsId(mateGoodsId);
        mateOutbound.setDeptId(sysUser.getCompanyId());

        MateOutbound mateOutbound1 = mateOutboundService.selectMateOutboundBy(mateOutbound);
        List<MateOutboundModel> mateOutboundModels = mateOutboundService.selectMateOutboundModelBy(mateGoodsId);
        if (mateOutboundModels.size() != 0) {
            resMap.put("mateOutbound", mateOutbound1);
        } else {
            mateOutbound1.setMateOutboundModels(mateOutboundModels);
            resMap.put("mateOutbound", mateOutbound1);
        }
        return resMap;
    }

    /**
     * 添加 采购订单入库信息
     */
    @GetMapping("/selectInformation")
    public Map<String, Object> add() {
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        Map<String, Object> resMap = new HashMap<>();
        //使用SimpleDateFormat的parse()方法生成Date
        SimpleDateFormat a = new SimpleDateFormat("yyyy-MM-dd");
        Date dates = null;
        try {
            dates = a.parse(a.format(new Date()));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        resMap.put("mateGoodsNum", mateProcurementService.orderNum(dates, 1));
        //用户主键
        resMap.put("sysUser", sysUser.getDeptId());
        //供应商
        MaterialManufactuter materialManufactuter = new MaterialManufactuter();
        materialManufactuter.setCompanyId(sysUser.getCompanyId());
        resMap.put("supplier", mateManufactuterService.selectMateManufactuter(materialManufactuter));
        resMap.put("userName", sysUserMapper.selectUserBycompanyId(sysUser.getCompanyId()));
        //用户
        //resMap.put("user", sysUserMapper.selectSalesmanList(sysUser.getCompanyId()));
        //税率
        Company company = companyMapper.selectByCompanyId(sysUser.getCompanyId());
        resMap.put("taxRate", company.getTaxRate());
        return resMap;
    }

    /**
     * 下拉表格
     */
    @GetMapping("/selectTableRow")
    public JSONObject tableSelect(@RequestParam(value = "materialId", required = false) Long materialId, @RequestParam(value = "mateProductType", required = false) String mateProductType) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", 0);
        jsonObject.put("msg", "");
        Map map = new HashMap<String, Object>();
        if (materialId != null & !"".equals(materialId)) {
            map.put("materialId", materialId);
            map.put("mateProductType", mateProductType);
            map.put("companyId", SecurityUtils.getLoginUser().getCompanyId());
        }
        List<MaterialProduct> products = mateProductService.selectProducts(map);
        JSONArray array = new JSONArray();
        for (MaterialProduct product : products) {
            if (product.getMateProductModel().size() != 0) {
                List<MateProductModel> productModels = product.getMateProductModel();
                for (MateProductModel productModel : productModels) {
                    JSONObject obj = new JSONObject();
                    obj.put("mateModelCode", productModel.getMateModelCode());
                    obj.put("mateModeno", productModel.getMateModeno());
                    obj.put("mateProductName", product.getMateProductName());
                    obj.put("mateproductId", product.getMateproductId());
                    obj.put("mateProductType", productModel.getMateProductType());
                    obj.put("warehousingNum", productModel.getWarehousingNum());
                    array.add(obj);
                }
            }
        }
        jsonObject.put("data", array);
        return jsonObject;
    }

    /**
     * 选择批号界面 库存列表
     */
    @GetMapping("/selectWarehousing")
    @ResponseBody
    public Map<String, Object> selectWarehousing(@RequestParam(value = "mateproductId", required = false) Long mateproductId,
                                                 @RequestParam(value = "mateProductType", required = false) String mateProductType) {
        Map<String, Object> resMap = new HashMap<>();
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("deptId", SecurityUtils.getLoginUser().getUser().getCompanyId());
        hashMap.put("mateproductId", mateproductId);
        hashMap.put("mateProductType", mateProductType);
        List<MateWarehousing> warehousing = mateOutboundService.selectSaleWarehousing(hashMap);
        resMap.put("warehousing", warehousing);
        return resMap;
    }

    @GetMapping("/selectProductModelLists")
    @ResponseBody
    public TableDataInfo selectProductModelLists(@RequestParam(value = "materialId", required = false) Long materialId,
                                                 @RequestParam(value = "mateProductName", required = false) String mateProductName,
                                                 @RequestParam(value = "mateProductType", required = false) String mateProductType) {
        startPage();
        Long deptId = SecurityUtils.getLoginUser().getUser().getCompanyId();
        MaterialProduct product = new MaterialProduct();
        product.setMaterialId(materialId);
        product.setCompanyId(deptId);
        product.setMateProductName(mateProductName);
        product.setMateProductType(mateProductType);
        return getDataTable(mateProcurementService.selectProductLists(product));
    }

}
